<template>
  <div style="height: calc(100vh - 60px); background-color: #ffffff">
    <el-tree
      :data="data"
      node-key="id"
      default-expand-all
      @node-drag-start="handleDragStart"
      @node-drag-enter="handleDragEnter"
      @node-drag-leave="handleDragLeave"
      @node-drag-over="handleDragOver"
      @node-drag-end="handleDragEnd"
      @node-drop="handleDrop"
      draggable
      highlight-current
      :allow-drop="allowDrop"
      :allow-drag="allowDrag"
      indent="30"
    >
    <span class="custom-tree-node" slot-scope="{ node, data }">
      <span>{{ node.data.code }}</span>
      <div style="width: 90%">
        <span style="text-align: left">{{ node.label }}</span>
      </div>
      <el-button v-if="node.data.level!=='2'" type="text" size="mini" @click="() => appendNode(node,data)">添加</el-button>
      <el-button v-if="node.data.level!=='0'" type="text" size="mini" @click="() => editNode(node,data)">修改</el-button>
      <el-button type="text" size="mini" @click="() => remove(data)">删除</el-button>
    </span>
    </el-tree>
  </div>
</template>

<script>
export default {
  data() {
    return {
      data: [{
        id: 1,
        label: '招商局集团',
        code: '100%',
        children: [
          { id: 1, label: '腾讯云科技有限公司', code: '30%' },
          { id: 2, label: '腾讯医疗健康（深圳）有限公司', code: '40%' },
          { id: 3, label: '深圳市腾讯视频文化传播有限公司', code: '30%' }
        ]
      }, {
        id: 2,
        label: '深圳腾富博投资有限公司',
        code: '100%',
        children: [
          { id: 5, label: '腾讯影业文化传播有限公司', code: '25%' },
          { id: 6, label: '霍尔果斯晓腾影业文化传播有限公司', code: '25%' },
          { id: 7, label: '北京腾讯影业有限公司', code: '25%' },
          { id: 8, label: '霍尔果斯腾影影视发行有限公司', code: '25%' }
        ]
      }, {
        id: 3,
        label: '北京魔方无限科技有限公司',
        code: '100%',
        children: [{
          id: 7,
          label: '北京像素软件科技股份有限公司',
          code: '50%'
        }, {
          id: 8,
          label: '浙江齐聚科技有限公司',
          code: '50%',
          children: [{
            id: 11,
            label: '北京腾新科技有限公司',
            code: '30%'
          }, {
            id: 12,
            label: '河北雄安新区腾讯计算机系统有限公司',
            code: '30%'
          }, {
            id: 13,
            label: '深圳市移卡科技有限公司',
            code: '40%'
          }]
        }]
      }],
      defaultProps: {
        children: 'children',
        label: 'label'
      }
    }
  },
  methods: {
    handleDragStart(node, ev) {
      console.log('drag start', node)
    },
    handleDragEnter(draggingNode, dropNode, ev) {
      console.log('tree drag enter: ', dropNode.label)
    },
    handleDragLeave(draggingNode, dropNode, ev) {
      console.log('tree drag leave: ', dropNode.label)
    },
    handleDragOver(draggingNode, dropNode, ev) {
      console.log('tree drag over: ', dropNode.label)
    },
    handleDragEnd(draggingNode, dropNode, dropType, ev) {
      console.log('tree drag end: ', dropNode && dropNode.label, dropType)
    },
    handleDrop(draggingNode, dropNode, dropType, ev) {
      console.log('tree drop: ', dropNode.label, dropType)
    },
    allowDrop(draggingNode, dropNode, type) {
      if (dropNode.data.label === '二级 3-1') {
        return type !== 'inner'
      } else {
        return true
      }
    },
    allowDrag(draggingNode) {
      return draggingNode.data.label.indexOf('三级 3-2-2') === -1
    }
  }
}
</script>

<style>
  .custom-tree-node {
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-size: 14px;
    padding-right: 8px;
  }
</style>
